package com.testdemo.common.base;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.testdemo.R;
import com.testdemo.common.utils.PageLoader;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lzy.okgo.OkGo;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 蔡勇 on 2017/11/27.
 *
 * 默认使用 R.layout.activity_base_list
 * 使用自定义布局时，重写 getActivityLayoutId()方法   ，布局中固定下拉组件和列表组件的id (与R.layout.activity_base_list中一致)
 * *****************  注意：在onCreate中不要写 setContentView(int layoutResId)，写了也是无效的  **************************
 */

public abstract class BaseListActivity<T> extends BaseActivity {



    @Bind(R.id.twinklingRefreshLayout)
    public TwinklingRefreshLayout mTwinklingRefreshLayout;

    @Bind(R.id.recyclerView)
    public RecyclerView mRecyclerView;

    public View mEmptyView;

    public BaseQuickAdapter mAdapter;

    public PageLoader<T> mPageLoader;


    private int activityLayoutId = 0;


    public boolean getEmptyViewEnable(){
     return true;
    }


    @Override
    public void setContentView(int layoutResId) {
        if (activityLayoutId == 0){
            activityLayoutId = layoutResId;
            super.setContentView(activityLayoutId);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getActivityLayoutId() == 0 ? R.layout.activity_base_list: getActivityLayoutId());
        ButterKnife.bind(this);
        initRecyclerView();
        setPageLoader();
        initTwinklingRefreshLayout();
        initView();

    }

    /**
     * 设置背景
     * @param resId
     */
    public void setBackground(int resId){
        View root_view = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        root_view.setBackgroundResource(resId);
    }

    public int getActivityLayoutId() {
        return 0;
    }

    /**
     * 提供分割线的方法
     *
     * @return
     */
    public RecyclerView.ItemDecoration getItemDecoration() {
        return new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL);
    }

    public abstract void initView();


    protected void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new BaseQuickAdapter<T, BaseViewHolder>(getItemLayoutId(), new ArrayList<T>()) {
            @Override
            protected void convert(final BaseViewHolder helper, final T item) {
                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onItemClick(helper, item);
                    }
                });
                setItemData(helper, item);
            }


        };
        if (getItemDecoration() != null) {
            mRecyclerView.addItemDecoration(getItemDecoration());
        }
        mRecyclerView.setAdapter(mAdapter);

    }

    private void setPageLoader() {
        mPageLoader = new PageLoader<T>(mAdapter, mAdapter.getData(), true) {
            @Override
            public void getData(int pageIndex, int pageSize, final int getDataType) {
                BaseListActivity.this.getData(pageIndex, pageSize, getDataType);
            }

            @Override
            public void showLoadingDialog() {
                showProgressDialog();
            }

            @Override
            public void dismissLoadingDialog() {
                dismissProgressDialog();
                mTwinklingRefreshLayout.finishLoadmore();
                mTwinklingRefreshLayout.finishRefreshing();
            }

            @Override
            public void saveCache(List<T> cacheData) {
            }

            @Override
            public List<T> getCache() {
                return null;
            }
        };

        if (mEmptyView == null) {
            if (getEmptyView() == null) {
                mEmptyView = LayoutInflater.from(mActivity).inflate(R.layout.view_empty, null);
            } else {
                mEmptyView = getEmptyView();
            }
            try {









                if (getEmptyViewEnable()) {
                    mPageLoader.setNoDateView(mEmptyView);
                }

            } catch (Exception e) {
            }

        }
    }

    private void initTwinklingRefreshLayout() {
        mTwinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                mPageLoader.getNextPage();
            }

            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                mPageLoader.update();
            }

            @Override
            public void onRefreshCanceled() {
                onGetDataCanceled();
            }

            @Override
            public void onLoadmoreCanceled() {
                onGetDataCanceled();
            }
        });
        mTwinklingRefreshLayout.setEnableOverScroll(false);
    }

    public void onGetDataCanceled() {
        try {
            mTwinklingRefreshLayout.finishRefreshing();
        } catch (Exception e) {
        }
        try {
            mTwinklingRefreshLayout.finishLoadmore();
        } catch (Exception e) {
        }
        OkGo.getInstance().cancelTag(this);
    }

    public abstract void onItemClick(BaseViewHolder holper, T item);

    public abstract int getItemLayoutId();

    public abstract void setItemData(BaseViewHolder holper, T item);

    public abstract void getData(int pageIndex, int pageSize, final int getDataType);

    public View getEmptyView() {
        return null;
    }
}
